﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using ECC_EXT_Entity;
using NHibernate;
using NHibernate.Criterion;

namespace ECC_EXT_DAO.Dao
{
    public class MailParagraghDao:DaoBase
    {

        public void Remove(object id)
        {
            using (ISession session = this.SessionFactory.OpenSession())
            {
                session.BeginTransaction();
                try
                {
                    
                    MailParagraghEntity _entity =
                                          session.Get<MailParagraghEntity>(id);

                    session.Delete(_entity);


                    session.Transaction.Commit();
                }
                catch (Exception ex)
                {
                    session.Transaction.Rollback();
                    throw ex;
                }

            }
        }

        public MailParagraghEntity GetByID(object id)
        {
            using (ISession session = this.SessionFactory.OpenSession())
            {
                return
                    session.Get<MailParagraghEntity>(id);
            }
        }

        public void ReOrder(string pEmailContentKeyID, int pType)
        {
            using (ISession session = this.SessionFactory.OpenSession())
            {
                session.BeginTransaction();
                ICriteria criteria =
                    session.CreateCriteria<MailParagraghEntity>();
                criteria.Add(Restrictions.Eq("EmailContentKeyID", pEmailContentKeyID));
                criteria.Add(Restrictions.Eq("Type", pType));
                criteria.AddOrder(Order.Asc("Paragragh"));

                IList<MailParagraghEntity> _list = criteria.List<MailParagraghEntity>();
                int count = 1;
                try
                {

                    foreach (var mailParagraghEntity in _list)
                    {
                        mailParagraghEntity.Paragragh = count++;
                        session.SaveOrUpdate(mailParagraghEntity);
                    }
                    
                    session.Transaction.Commit();
                }
                catch (Exception ex)
                {
                    session.Transaction.Rollback();
                    throw ex;
                }

            }
        }

        public void ReOrder(string pEmailContentKeyID, int pType,int checkIndex)
        {
            using (ISession session = this.SessionFactory.OpenSession())
            {
                session.BeginTransaction();
                ICriteria criteria =
                    session.CreateCriteria<MailParagraghEntity>();
                criteria.Add(Restrictions.Eq("EmailContentKeyID", pEmailContentKeyID));
                criteria.Add(Restrictions.Eq("Type", pType));
                criteria.AddOrder(Order.Asc("Paragragh"));

                IList<MailParagraghEntity> _list = criteria.List<MailParagraghEntity>();

                if (_list.Count<2)
                    return;

                MailParagraghEntity tMailParagraghEntity = _list[0];
                try
                {

                    foreach (var mailParagraghEntity in _list)
                    {

                        if (mailParagraghEntity.Paragragh == checkIndex)
                        {
                            mailParagraghEntity.Paragragh = checkIndex - 1;
                            session.SaveOrUpdate(mailParagraghEntity);

                            tMailParagraghEntity.Paragragh = checkIndex;
                            session.SaveOrUpdate(tMailParagraghEntity);

                            break;
                        }

                        tMailParagraghEntity = mailParagraghEntity;
                    }

                    session.Transaction.Commit();
                }
                catch (Exception ex)
                {
                    session.Transaction.Rollback();
                    throw ex;
                }

            }
        }

        public void Add(MailParagraghEntity _mailParagraghEntity)
        {
            using (ISession session = this.SessionFactory.OpenSession())
            {
                session.BeginTransaction();
                
                ICriteria criteria =
                    session.CreateCriteria<MailParagraghEntity>();

                criteria.Add(Restrictions.Eq("EmailContentKeyID", _mailParagraghEntity.EmailContentKeyID));
                criteria.Add(Restrictions.Eq("Type", _mailParagraghEntity.Type));
                criteria.AddOrder(Order.Desc("Paragragh"));

                IList<MailParagraghEntity> _list =
                    criteria.List<MailParagraghEntity>();

                if (_list.Count == 0)
                    _mailParagraghEntity.Paragragh = 1;
                else
                {
                    _mailParagraghEntity.Paragragh = _list[0].Paragragh+1;
                }


                session.SaveOrUpdate(_mailParagraghEntity);
                session.Transaction.Commit();
            }
        }

        public void SaveOrUpdate(MailParagraghEntity _mailParagraghEntity)
        {
            using (ISession session = this.SessionFactory.OpenSession())
            {
                session.BeginTransaction();
                session.SaveOrUpdate(_mailParagraghEntity);
                session.Transaction.Commit();
            }
        }

        public IList<MailParagraghEntity> Search(string pEmailContentKeyID, int pType)
        {
            using (ISession session = this.SessionFactory.OpenSession())
            {
                ICriteria criteria =
                    session.CreateCriteria<MailParagraghEntity>();
                criteria.Add(Restrictions.Eq("EmailContentKeyID", pEmailContentKeyID));
                criteria.Add(Restrictions.Eq("Type", pType));
                criteria.AddOrder(Order.Asc("Paragragh"));
                return
                criteria.List<MailParagraghEntity>();
            }
        }
    }
}
